﻿using System;

using System.Linq;
using System.Text;

namespace GameLibrary
{   
    using Interfaces;
    [Serializable]
    public struct Position
    {
        #region static menbers
        public static readonly Position Zero = new Position(0, 0);
        public static readonly Position MinusOne = new Position(-1, -1);
        public static readonly Position One = new Position(1, 1);
        #endregion
        #region variables
        public int X;
        public int Y;
        public int AbsX
        {
            get
            {
                if (X >= 0)
                    return X;
                else
                    return -X;
            }
        }
        public int AbsY
        {
            get
            {
                if (Y >= 0)
                    return Y;
                else
                    return -Y;
            }
        }
        #endregion
        #region constructor
        public Position(int x, int y)
        {
            this.X = x;
            this.Y = y;
        }
        #endregion
        #region find distance
        /// <summary>
        /// find the distance from a to b
        /// the distance is counted as eight way.
        /// <example>
        /// a=(0,0) b=(1,0) distance = 1
        /// a=(0,0) b=(1,1) distance = 1
        /// a=(0,0) b=(2,1) distance = 2
        /// </example>
        /// </summary>
        /// <param name="a">first position</param>
        /// <param name="b">second position</param>
        /// <returns>
        /// eight way distance
        /// return > 0;
        /// </returns>
        public int FindDistance(Position p)
        {
            int tmpX = Math.Abs(this.X - p.X);
            int tmpY = Math.Abs(this.Y - p.Y);
            return (tmpX >= tmpY) ? tmpX : tmpY;
        }
        #endregion
        #region -
        public static Position operator -(Position a, Position b)
        {
            return new Position(a.X - b.X, a.Y - b.Y);
        }
        #endregion
        #region +
        public static Position operator +(Position a, Position b)
        {
            return new Position(a.X + b.X, a.Y + b.Y);
        }
        #endregion
        #region ==
        public static bool operator ==(Position a, Position b)
        {
            return (a.X == b.X) && (a.Y == b.Y);
        }
        #endregion
        #region !=
        public static bool operator !=(Position a, Position b)
        {
            return !(a == b);
        }
        #endregion
        #region override
        public override bool Equals(object obj)
        {
           if(obj is Position)
           {
                Position tmp = (Position)obj;
                if(this == tmp)
                  return true;
                else
                  return false;
           }
           else
                return false;
        }
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
        public override string ToString()
        {
            //return base.ToString();
            return "Position "
                + " X " + this.X
                + " Y " + this.Y + '\n';
        }
        #endregion
        #region is less than zero
        /// <summary>
        /// Return true if any of the variable in position less then 0.
        /// else return false.
        /// </summary>
        /// <returns></returns>
        public bool IsLessThanZero()
        {
            if (this.X < 0 || this.Y < 0)
                return true;
            else
                return false;
        }
        #endregion

        #region get abs sum
        /// <summary>
        /// Return the sum of absolute value of x and y
        /// </summary>
        /// <returns></returns>
        public int GetAbsSum()
        {
            int tmp = 0;
            if (this.X >= 0)
                tmp += X;
            else
                tmp -= X;
            if (this.Y >= 0)
                tmp += Y;
            else tmp -= Y;
            return tmp;
        }
        #endregion
    }
}
